package com.atguigu.gulimall.product.config;


import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;


@Configuration
public class MyRedisConfig {

    private RedisConnectionFactory connectionFactory = null;

    @Bean(name = "RedisConnectionFactory")
    public RedisConnectionFactory initRedisConnectionFactory(){
        if(this.connectionFactory != null)
            return this.connectionFactory;
        //创建Jedis连接池
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        {
            //设置最大空闲数
            poolConfig.setMaxIdle(30);
            //设置最大连接总数
            poolConfig.setMaxTotal(50);
            //设置最大等待毫秒数
            poolConfig.setMaxWaitMillis(2000);
        }
        //创建Jedis连接工厂
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig);
        //创建Redis脱机配置
        RedisStandaloneConfiguration rscg = connectionFactory.getStandaloneConfiguration();
        {
            //设置主机
            rscg.setHostName("192.168.10.110");
        }
        this.connectionFactory = connectionFactory;
        return connectionFactory;
    }

    /**
     * Author : hmy
     * @date 2021/8/22 16:38
     * desc : 所有对redissson都是通过对redisclent操作的
     **/
    @Bean(destroyMethod="shutdown")
    RedissonClient redisson() throws IOException {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.10.110:6379");
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
    @Bean(name = "redisTemplate")
    public RedisTemplate<Object,Object> initRedisTemplate(){
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //RedisTemplate会自动初始化StringRedisSerializer，所以这里直接获取
//        RedisSerializer stringSerializer = redisTemplate.getStringSerializer();
        redisTemplate.setKeySerializer(new GenericFastJsonRedisSerializer());
        redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new GenericFastJsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        redisTemplate.setConnectionFactory(initRedisConnectionFactory());
        return redisTemplate;
    }
}



